<template>
	<view class="padb20">
		<stop isBack>
			<view slot="content">物资领取</view>
		</stop>
		<view class="mart3">
			<stab @change="(index)=>{curTab=index}" :names="['出库单','绑定设备','绑定维修单','绑定保养单']"></stab>
		</view>
		<view class="padlr3" v-show="curTab == 0">
			<view class="lh2 bcf pad2 bdtso mart3">
				<view class="list-style-2 flrc">
					<view class="flex1">入库单信息</view>
				</view>
				<view class="lh2 bcf pad2 mart3">
					<sentity label="选择仓库" model="Store" placeholder="请选择" v-model="en.store"></sentity>
				</view>
				<view class="lh2 bcf pad2 mart3 bdtda">
					<sentity label="选择用途" model="MachinePartUsage" placeholder="请选择" v-model="en.usagee"></sentity>
				</view>
				<view class="lh2 bcf pad2 bdtda">
					<stextarea label="基本信息" placeholder="填写领取物资的主要使用用途文字说明" v-model="en.tips"></stextarea>
				</view>
			</view>
			<view class="lh2 bcf pad2 bdtso mart3">
				<view class="list-style-2 flrc">
					<view class="flex1">选择物资</view>
					<!-- <view class="cu-btn line-green round" @tap="showSelect" >添加</view> -->
					<view class="cu-btn line-green round marr2" @tap="scanStoreItem" >
						<text class="cuIcon-scan"></text>
						<ripple></ripple>
					</view>
					<view class="cu-btn line-green round" @tap="showSelect" >
						添加
						<ripple></ripple>
					</view>
				</view>
				<!-- 显示选择的物资 -->
				<view class="flcc bdso mart3" v-for="item,index in en.items" :key="index"
				v-if="isbool(en.items)">
					<store-item class="w100 bc13 pad1" :iri="item"></store-item>
					<view class="w100 bdtdo bc13">
						<stext label="数量" placeholder="请输入数量" v-model="nums[index]"></stext>
					</view>
				</view>
				<view class="pad3" v-if="en.items.length === 0">
					<view class="bdda tac pad5">未选择任何数据</view>
				</view>
			</view>
		</view>
		<view class="padlr3" v-show="curTab == 1">
			<view class="lh2 bcf pad2 bdtso mart3">
				<machine-picker v-model="en.machine"></machine-picker>
			</view>
		</view>
		<view class="padlr3" v-show="curTab == 2">
			<view class="lh2 bcf pad2 bdtso mart3">
				<stext v-model="en.fix" label="维修单号" placeholder="请输入单号"></stext>
			</view>
		</view>
		<view class="padlr3" v-show="curTab == 3">
			<view class="lh2 bcf pad2 bdtso mart3">
				<stext v-model="en.maintain" label="保养单号" placeholder="请输入单号"></stext>
			</view>
		</view>
		<store-item-selector :show="showSelectList" @onSelect="selectStoreItem"></store-item-selector>
		<view class="lh2 bcf pad2 bdtso pos-f l0 r0 b0">
			<view class="tac pad2 line-green round cu-btn lg w100 " @tap="submit">
				<view>提交</view>
				<ripple></ripple>
			</view>
		</view>
	</view>
</template>

<script>
	import machinePicker from '../../templete/machine/machine_picker.vue'
	import StoreItem from '../../templete/store/store_item.vue'
	import StoreItemSelector from '../../templete/store/store_item_selector.vue'
	export default {
		components:{machinePicker,StoreItem,StoreItemSelector},
		data() {
			return {
				curTab:0,
				id:"",
				showSelectList:false,
				en:{
					machine:'',
					usagee:'',
					store:'',
					tips:'',
					items:[],
					fix:'',
					maintain:''
				},
				nums:[],
				machine:'',
				bsn:'',
				msn:''
			}
		},
		methods: {
			showSelect(){
				this.showSelectList = Math.random()
			},
			selectStoreItem(val){
				this.en.items.push(val.iri)
				this.nums.push("1")
			},
			scanStoreItem(){
				this.$dev.scan(info=>{
					this.$api.getRes(info,en=>{
						if(this.$com.isTrue(en)){
							this.en.items.push(info)
							this.nums.push("1")
						}
					})
					
				},(err)=>{
				})
			},
			submit(){
				this.log(this.en)
				//检查数据
				if(this.isnull(this.en.usagee)){this.$com.showToast('请选择用途类型');return;}
				if(this.isnull(this.en.store)){this.$com.showToast('请选择仓库');return;}
				if(this.isnull(this.en.items)){this.$com.showToast('没有选择任何物资');return;}
				this.$api.post("/api/machinePartOrders",{
					type:this.en.type,
					store:this.en.store,
					tips:this.en.tips,
					items:this.en.items,
					nums:this.nums,
					machine:this.en.machine,
					fix:this.en.fix,
					maintain:this.en.maintain,
				},(res)=>{
					this.log(res)
					if(res.s == 1){
						this.goback()
					}
				})
			}
		},
		onLoad(option) {
				this.clearLoading()
				this.id = option.id
				this.log(option)
				setTimeout(()=>{
					
				},1000)
				this.machine = option.machine
				this.bsn = option.bsn
				this.msn = option.msn
		},
		mounted() {
			let machine = this.machine
			if(this.$com.isbool(machine)){
				//从维修单过来的
				this.en.machine = '/api_/machines/'+machine
			}
			let bsn = this.bsn
			if(this.$com.isbool(bsn)){
				//从维修单过来的
				this.en.fix = bsn
			}
			let msn = this.msn
			if(this.$com.isbool(msn)){
				//从维修单过来的
				this.en.maintain = msn
			}
			this.log(this.en)
		},
		watch:{
			nums(va){
				this.log(va)
			}
		}
	}
</script>

<style>

</style>
